<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>JOB配置</title>
    <link rel="stylesheet" href="../layui/css/layui.css">
</head>
<body>
<!-- 你的HTML代码 -->
<div class="demoTable">
    <form class="layui-form">
        任务名称：
        <div class="layui-inline">
            <input class="layui-input" name="jobName"  autocomplete="off">
        </div>
        <button class="layui-btn layui-btn-primary" lay-submit lay-filter="addJobCfg"  >新增任务</button>
        <button class="layui-btn layui-btn-primary" lay-submit lay-filter="recovery"  >全部恢复</button>
        <button class="layui-btn layui-btn-primary" lay-submit lay-filter="stopAll"  >全部停止</button>
        <button class="layui-btn" lay-submit lay-filter="reload"  >搜索</button>
    </form>
    <hr>
</div>

<table class="layui-hide" id="LAY_table_user" lay-filter="LAY_table_user"></table>

<script type="text/html" id="toolbarJobCfg">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">详情</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="log">日志</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="stop">停止</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="restart">重启</a>
    <a class="layui-btn layui-btn-xs" lay-event="cfgRpt">配置Report</a>
    <a class="layui-btn layui-btn-xs" lay-event="cfgUser">通知用户</a>
</script>

<script src="../layui/layui.all.js"></script>
<script>

    var tableOption =  {
                elem: '#LAY_table_user'
                ,data:[]
                ,cols: [[
                    {field:'id', title: 'ID', width:120, sort: true }
                    ,{field:'jobName', title: '任务名称', width:180,sort: true}
                    ,{field:'startTime', title: '开始时间', width:180, sort: true}
                    ,{field:'lastTime', title: '结束时间', width:180, sort: true}
                    // ,{field:'period', title: '频率', width:80}
                    // ,{field:'unit', title: '单位',width:120}
                    ,{field:'state', title: '状态', sort: true, width:120}
                    ,{field:'active', title: '有效', sort: true, width:80,templet:function(d){
                        var active  =   '<input type="checkbox" name="active" lay-filter="active" value="'+d.id+'" lay-skin="switch"   >'
                        if(d.active == "Y"){
                            active =   '<input type="checkbox" name="active"  lay-filter="active" value="'+d.id+'" lay-skin="switch"   checked  >'
                        }
                        return active
                    }}
                    ,{title: '操作', sort: true, toolbar:"#toolbarJobCfg"}
                ]]
                ,id: 'LAY_table_user'
                ,page: true
                ,height: 'full-230'
            }

    //由于模块都一次性加载，因此不用执行 layui.use() 来加载对应模块，直接使用即可：
            ;!function(){
        var layer = layui.layer
                ,form = layui.form;
        var table = layui.table;
        var $ = layui.$

        var jobCfgUtil = {
            query:function(table,tableOption,obj){
                $.getJSON("../xxdsqljob/index",obj, function(rst){
                    if(rst.code!=0){
                        layer.alert(rst.msg)
                    }else{
                        tableOption.data = rst.data
                        table.render(tableOption)
                    }
                })
            }
        }

        form.on("submit(addJobCfg)",function(obj){
            layer.open({
                title:"新增任务",
                type: 2,
                offset: '30px',
                area: ['50%', '60%'],
                content: 'jobCfgAdd.html',
                cancel: function(index, layero){
                    jobCfgUtil.query(table,tableOption,{jobName:function(){return $("input[name=jobName]").val()}})
                }
            });
            return false
        })

        form.on('switch(active)', function(data){
            var flag = data.elem.checked?"Y":"N";
            var jobId = data.value;
            if(flag == "N"){
                layer.confirm("确定要失效并停止当前任务吗？",{icon: 3, title:'提示'},function(index){
                    layer.close(index)
                    $.getJSON("../xxdsqljob/active?jobId="+jobId,{active:flag},function(rst){
                        if(rst.code!=0){
                            layer.alert(rst.msg,{icon:2})
                        }
                    })
                })
            }else{
                layer.confirm("激活任务，需重启执行",{icon: 3, title:'提示'},function(index){
                    layer.close(index)
                    $.getJSON("../xxdsqljob/active?jobId="+jobId,{active:flag},function(rst){
                        if(rst.code!=0){
                            layer.alert(rst.msg,{icon:2})
                        }
                    })
                })
            }
        });


        table.on('tool(LAY_table_user)',function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event;
            switch (layEvent){
                case "edit":
                    layer.open({
                        title:"定时任务详情",
                        type: 2,
                        offset: '30px',
                        area: ['50%', '60%'],
                        content: 'jobCfgEdit.html?jobId='+data.id,
                        cancel: function(index, layero){
                            jobCfgUtil.query(table,tableOption,{jobName:function(){return $("input[name=jobName]").val()}})
                        }
                    });
                    break;
                case "log":
                    layer.open({
                        title:"日志信息:"+data.jobName,
                        type: 2,
                        offset: '30px',
                        area: ['90%', '70%'],
                        content: 'jobCfgLog.html?jobId='+data.id,
                        cancel: function(index, layero){
                            jobCfgUtil.query(table,tableOption,{jobName:function(){return $("input[name=jobName]").val()}})
                        }
                    });
                    break;
                case "stop":
                    layer.confirm("确定要停止当前任务吗？",{icon: 3, title:'提示'},function(index){
                        $.getJSON("../xxdsqljob/stopJob?jobId="+data.id,function(rst){
                            if(rst.code!=0){
                                layer.alert(rst.msg,{icon:2})
                            }else{
                                layer.msg(rst.msg)
                            }
                        })
                        layer.close(index);
                    })

                    break;
                case "restart":
                    layer.confirm("重启任务将先停止当前任务，然后重启？",{icon: 3, title:'提示'},function(index){
                        $.getJSON("../xxdsqljob/restart?jobId="+data.id,function(rst){
                            if(rst.code!=0){
                                layer.alert(rst.msg,{icon:2})
                            }else{
                                layer.msg(rst.msg)
                            }
                        })
                        layer.close(index);
                    })
                    break;
                case "cfgRpt":
                    layer.open({
                        title:"配置Report："+data.jobName,
                        type: 2,
                        offset: '30px',
                        area: ['90%', '70%'],
                        content: 'jobReport.html?jobId='+data.id,
                        cancel: function(index, layero){
                            //jobCfgUtil.query(table,tableOption,{jobName:function(){return $("input[name=jobName]").val()}})
                        }
                    });
                    break;
                case "cfgUser":
                    layer.open({
                        title:"配置Report："+data.jobName,
                        type: 2,
                        offset: '30px',
                        area: ['70%', '70%'],
                        content: 'jobUser.html?jobId='+data.id,
                        cancel: function(index, layero){
                            //jobCfgUtil.query(table,tableOption,{jobName:function(){return $("input[name=jobName]").val()}})
                        }
                    });
                    break;
            }
            return false
        })


        form.on("submit(reload)",function(obj){
            //console.log(obj.field)
            jobCfgUtil.query(table,tableOption,obj.field)
            return false
        })

        form.on("submit(recovery)",function(obj){
            layer.confirm("此操作将重启所有未执行的任务？",{icon: 3, title:'提示'},function(index){
                $.getJSON("../xxdsqljob/recovery",function(rst){
                    if(rst.code!=0){
                        layer.alert(rst.msg,{icon:2})
                    }else{
                        jobCfgUtil.query(table,tableOption,obj.field)
                        layer.msg(rst.msg)
                    }
                })
            })
            return false
        })

        form.on("submit(stopAll)",function(obj){
            layer.confirm("此操作将停止所有任务，但部分任务可能会停止失败？",{icon: 3, title:'提示'},function(index){
                $.getJSON("../xxdsqljob/stopAll",function(rst){
                    if(rst.code!=0){
                        layer.alert(rst.msg,{icon:2})
                    }else{
                        jobCfgUtil.query(table,tableOption,obj.field)
                        layer.msg(rst.msg)
                    }
                })
            })
            return false
        })

        jobCfgUtil.query(table,tableOption)
    }();

</script>

</body>
</html>
